package com.banciyuan.bcywebview.biz.exhibition;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.util.BaseObserver;
import com.banciyuan.bcywebview.utils.common.UIUtils;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.Location;

/**
 * Created by squall on 2016/9/6.
 */
public class ExhibitionLocationAdapter extends BaseAdapter {

    private Context mContext;
    private List<Location> llist = new ArrayList<>();
    private LayoutInflater inflater;
    private String city;

    public ExhibitionLocationAdapter(List<Location> llist, Context mContext, String city) {
        this.llist = llist;
        this.city = city;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return llist.size();
    }

    @Override
    public Object getItem(int position) {
        return llist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHoder {
        public TextView category;
        public LinearLayout line;

        public ViewHoder(View view) {
            category = (TextView) view.findViewById(R.id.category_tv);
            line = (LinearLayout) view.findViewById(R.id.line);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHoder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.activity_exhibtion_location, null);
            viewHolder = new ViewHoder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHoder) convertView.getTag();
        }

        Location location = llist.get(position);
        if (!TextUtils.isEmpty(location.getType_name())) {
            viewHolder.category.setVisibility(View.VISIBLE);
            viewHolder.category.setText(location.getType_name());
        } else {
            viewHolder.category.setVisibility(View.GONE);
        }

        final List<Location.Local> data = location.getData();

        viewHolder.line.removeAllViews();
        if (!data.isEmpty()) {
            viewHolder.line.setVisibility(View.VISIBLE);
            int row = data.size() % 4 == 0 ? data.size() / 4 : data.size() / 4 + 1;
            for (int i = 0; i < row; i++) {
                LinearLayout tr = new LinearLayout(mContext);
                LinearLayout.LayoutParams llrow = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                llrow.setMargins(0, UIUtils.dip2px(12, mContext), 0, 0);
                tr.setLayoutParams(llrow);

                tr.setOrientation(LinearLayout.HORIZONTAL);
                for (int j = 0; j < 4; j++) {
                    TextView tv = null;
                    final int index = j + (i * 4);
                    if (index < data.size()) {
                        String temp_city = data.get(index).getCity();
                        if (!temp_city.equals(city)) {
                            tv = (TextView) inflater.inflate(R.layout.exhibtion_location_text_item, null);
                            tv.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    BaseObserver.getInstance().notifyWatchers(BaseObserver.CITY_CILICK, data.get(index));
                                }
                            });
                        } else {
                            tv = (TextView) inflater.inflate(R.layout.exhibtion_location_text_item_select, null);
                        }
                        tv.setVisibility(View.VISIBLE);
                        tv.setText(temp_city);
                    } else {
                        tv = (TextView) inflater.inflate(R.layout.exhibtion_location_text_item, null);
                        tv.setVisibility(View.INVISIBLE);
                    }
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, UIUtils.dip2px(30, mContext));
                    layoutParams.setMargins(UIUtils.dip2px(8, mContext), 0, UIUtils.dip2px(8, mContext), 0);
                    layoutParams.weight = 1;
                    tv.setLayoutParams(layoutParams);
                    tr.addView(tv);
                }
                viewHolder.line.addView(tr);
            }
        } else {
            viewHolder.line.setVisibility(View.GONE);
        }

        return convertView;
    }
}
